<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品单位类
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   allen.qiang
 * @version 1.0
 */

class goodsUnit {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 添加单位
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function saveUnitInfo( $arg_array ){
        //检查参数
        if (!isset($arg_array['unitName'] ,$arg_array['sortOrder'] ,$arg_array['creator'], $arg_array['useFlg'],$arg_array['unitID']))
            return array( 'code' => 400, 'message' => '参数错误');

        $sortOrder = $arg_array['sortOrder'] ;			//排序序号
        $unitID	   = $arg_array['unitID'] ;				//单位号
        $tagID 	   = 0 ;
        $unitInfo  = new entity('goods_unit') ;
        $unitName   = trim($arg_array['unitName']) ;
        $this->dbca->beginTransaction();
        try{
            //如果单位号不为空则查询检查单位号
            if($unitID != 0){
                $unitInfo->unitID = $unitID ;
                if(!$this->dbca->fetch($unitInfo))
                    throw new Exception(sprintf('单位号「%d」,的单位不存在,请审核后再操作',$str_array[0]),400) ;

                //检查单位排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_unit WHERE sortOrder=? AND unitID!=?','ii' ,$sortOrder, $unitID) ;
                if($issetSO[0]->count > 0)
                    throw new Exception('排序序号已经被使用',400) ;

                $unitInfo->editor = $arg_array['creator'] ;
                $tagID = $unitInfo->tagID ;

                //修改商品列表中的单位名称
                $this->dbca->stmt_query('UPDATE goods_list SET TIMESTAMP=NOW(), unitName=? WHERE unitID=?','si' ,$unitName, $unitID) ;
                $this->dbca->stmt_query('UPDATE goods_list_edit SET TIMESTAMP=NOW(), unitName=? WHERE unitID=?','si' ,$unitName, $unitID) ;
                $this->dbca->stmt_query('UPDATE goods_sale SET TIMESTAMP=NOW(), unitName=? WHERE unitID=?','si' ,$unitName, $unitID) ;
                $this->dbca->stmt_query('UPDATE goods_sale_edit SET TIMESTAMP=NOW(), unitName=? WHERE unitID=?','si' ,$unitName, $unitID) ;

            }else {
                //检查单位排序有没有被使用
                $issetSO = $this->dbca->stmt_query('SELECT COUNT(*) count FROM goods_unit WHERE sortOrder=?','i' ,$sortOrder) ;
                if($issetSO[0]->count > 0)
                    throw new Exception('排序序号已经被使用',400) ;

                $unitInfo->creator = $arg_array['creator'] ;
            }

            //生成单位标签
            $tag_array  = array('tagName'	 => $unitName ,
                'tagID'		 => $tagID ,
                'tagGroupID' => UNIT_TAGGROUPID ,
                'creator' 	 => $arg_array['creator']) ;

            $resultData = $this->commPropertyTag ($tag_array) ;
            if($resultData['code'] == 400)
                throw new Exception($resultData['message'],400) ;

            //保存和更新单位信息
            $unitInfo->tagID  	 = $resultData['message'];
            $unitInfo->unitName  = $unitName ;
            $unitInfo->useFlg	 = $arg_array['useFlg'] ;
            $unitInfo->sortOrder = $arg_array['sortOrder'] ;
            $this->dbca->save( $unitInfo , 'goods' ) ;

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => '单位保存成功' );
        }catch (Exception $e) {
            $this->dbca->rollBack();
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage());
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 删除单位
     *
     */
    public function deleteUnit ( $arg_array ) {
        //单位号串检查
        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的单位' );

        //将单位号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);

        $tagID_str = '' ;
        try {
            //循环删除单位
            foreach( $delete_array as &$u_l ) {
                if (!is_numeric( $u_l ))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询单位信息
                $unitInfo = new entity('goods_unit');
                $unitInfo->unitID = $u_l;
                if ($this->dbca->fetch( $unitInfo )) {
                    $this->dbca->delete( $unitInfo );

                    //删除品牌标签
                    if($tagID_str == '' && $unitInfo->tagID != '')
                        $tagID_str .= $unitInfo->tagID ;

                    else if($tagID_str != '' && $unitInfo->tagID != '')
                        $tagID_str .= '_' . $unitInfo->tagID ;
                }

            }

            //删除品牌标签
            $this->deleteTag(array('delete_str' => $tagID_str)) ;

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {
            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );
        }
    }

    /**
     *单位的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function actionUnit ( $arg_array ) {
        //参数检查
        if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        $str_array = explode('_', $arg_array['useFlg_str']);

        //查询单位是否存在或已经被操作
        $unitInfo = new entity('goods_unit');
        $unitInfo->unitID = $str_array[0] ;
        if (!$this->dbca->fetch( $unitInfo ))
            return array( 'code' => 400, 'message' => sprintf('单位号「%d」,的单位不存在,请审核后再操作',$str_array[0]));

        //检查单位状态是否已经被修改
        if($unitInfo->useFlg == $str_array[1] && $str_array[1] == 1)
            return array( 'code' => 400, 'message' => sprintf('单位号「%d」,的单位已启用',$str_array[0]));

        if($unitInfo->useFlg == $str_array[1] && $str_array[1] == 0)
            return array( 'code' => 400, 'message' => sprintf('单位号「%d」,的单位已禁用',$str_array[0]));

        //保存和更新单位
        $unitInfo->useFlg = $str_array[1] ;
        $this->dbca->save( $unitInfo , 'goods' );

        if($str_array[1] == 1)
            return array( 'code' => 200, 'message' => sprintf('单位号「%d」,的单位启用成功',$str_array[0]) );

        else if ($str_array[1] == 0)
            return array( 'code' => 200, 'message' => sprintf('单位号「%d」,的单位禁用成功',$str_array[0]) );
    }

}